#target "illustrator-22.064"

if (app.documents.length > 0 && app.activeDocument.pathItems.length > 0) {
  var CURR_DOC = app.activeDocument;
  var CURR_ARTBOARD_INDEX = CURR_DOC.artboards.getActiveArtboardIndex()
  var CURR_ARTBOARD = CURR_DOC.artboards[CURR_ARTBOARD_INDEX].artboardRect

  // setLogoGroup()
  setYiqiGroup()
}

/* ============================================================ */
function setLogoGroup() {
  var artboardSize = _getArtboardSize(CURR_ARTBOARD)
  var artboardShortSide = artboardSize.width < artboardSize.height ? artboardSize.width : artboardSize.height
  var logoGroupScaleRatio = _getLogoRatio(artboardSize)
  var LOGO_RATIO = 0.526314
  var logoGroupHeight = artboardShortSide * logoGroupScaleRatio
  var distance = logoGroupHeight * LOGO_RATIO
  _setSizeAndPosition({x: artboardSize.x + distance, y: artboardSize.y - distance}, logoGroupHeight)
}

function setYiqiGroup() {
  var artboardSize = _getArtboardSize(CURR_ARTBOARD)
  var artboardShortSide = artboardSize.width < artboardSize.height ? artboardSize.width : artboardSize.height
  var logoGroupScaleRatio = _getLogoRatio(artboardSize)
  var LOGO_RATIO = 0.526314
  var logoGroupHeight = artboardShortSide * logoGroupScaleRatio
  var yiqiGroupHeight = logoGroupHeight * getYiqiRatio(artboardSize)
  var distance = logoGroupHeight * LOGO_RATIO
  var x = artboardSize.x + artboardSize.width - (yiqiGroupHeight * 1.2596) - distance;
  var y = artboardSize.y - artboardSize.height + yiqiGroupHeight + (distance / 2)
  _setSizeAndPosition({x: x, y: y}, yiqiGroupHeight)
}

function _getLogoRatio(rectangle) {
    var ratioRange =  [1,    4/3,  7/5,  3/2,  16/9, 2,   3,   5,   9,   10];
    var logoRatios = [[0.4,  0.4,  0.4,  0.4,  0.4,  0.4, 0.4, 0.4, 0.4, 0.4],
                      [0.15, 0.15, 0.15, 0.15, 0.15, 0.2, 0.2, 0.3, 0.3, 0.4],
                      [0.15, 0.15, 0.15, 0.15, 0.15, 0.2, 0.2, 0.3, 0.3, 0.4]];
    var W = rectangle.width || 1;
    var H = rectangle.height || 1;
    var rectRatio = W > H ? W / H : H / W;
    var ratioIndex = _getArrIndex_(ratioRange, rectRatio) - 1
    var typesRange = [130, 560]
    var shortSide = W < H ? W : H;
    var typeIndex = _getArrIndex_(typesRange, shortSide)
    return logoRatios[typeIndex][ratioIndex]
}

function getYiqiRatio(rectangle) {
  /*
  var ratioRange =  [1,    4/3,  7/5,  3/2,  16/9, 2,   3,   5,   9,   10];
  var yiqiRatios = [[0.8,  0.8,  0.8,  0.8,  0.8,  0.8, 0.8, 0.8, 0.8, 0.8],
                    [0.6,  0.6,  0.6,  0.6,  0.6,  0.6, 0.6, 0.6, 0.6, 0.6]]
  var W = rectangle.width || 1;
  var H = rectangle.height || 1;
  var rectRatio = W > H ? W / H : H / W;
  var ratioIndex = _getArrIndex_(ratioRange, rectRatio) - 1
  */
  // var typesRange = [240, 560]
  var W = rectangle.width || 1;
  var H = rectangle.height || 1;
  var shortSide = W < H ? W : H;
  return shortSide < 240 ? 0.8 : 0.6
  // var typeIndex = _getArrIndex_(typesRange, shortSide)
  // return yiqiRatios[typeIndex][ratioIndex]
}

/* ================== Public Base Function ========================================== */

// functions
// setSizeAndPosition({x: -100, y: 200}, 100)
/* todo 默认以px为单位，需要增加单位判断 */
function _setSizeAndPosition (position, height) {
  var doc = app.activeDocument;
  if (doc.selection.length < 1) {
    alert(':-) 请选好图形后，再试试')
    return
  }
  var curr = doc.selection[0]
  var ratio = curr.width / curr.height || 1

  if  (position) {
    curr.left = position.x
    curr.top = position.y
  }
  if (height) {
    curr.height = height
    curr.width = height * ratio
  }
}

function _getArtboardSize(canvas) {
    var rect = canvas.toString().split(',')
    var x1 = +rect[0]
    var y1 = +rect[1]
    var x2 = +rect[2]
    var y2 = +rect[3]
    var rectangle = {
        y: y1,
        x: x1,
        width: Math.abs(x2 - x1),
        height: Math.abs(y2 - y1)
    }
    return rectangle
}

/* ================== Js untis Functions ========================================== */

function _getArrIndex_(arr, num) {
  var index = 0;
  var len = arr.length
  if (num >= arr[len - 1]) {
    return len
  }
	while(arr[index] <= num) {
		index += 1;
	}
	return index;
}

/* ================== Public Base Function ========================================== */
